<?php

declare(strict_types=1);

namespace app\admin\controller;

use addons\Builder\facade\Builder;
use think\facade\Cache;

class News extends Base
{
    //主键，默认为id
    protected string $pk = 'id';

    //主模型
    protected string $model = '\app\common\model\News';

    //禁用回车提交
    protected bool $enterSubmit = false;
    /**
     * 健康管理
     */
    public function health()
    {
        return $this->index(2);
    }
    /**
     * 动态资讯
     */
    public function dynamic()
    {
        return $this->index(3);
    }
    /**
     * 系统公告
     */
    public function notice()
    {
        return $this->index(1);
    }
    public function index($type): string
    {
        Cache::set('news_type' , $type);
        return Builder::Table()
            ->autoStructure($this->model, $this->pk, 'barDemo,140')
            ->addRightButtons(['edit' => ['size' => '60%'], 'delete'])
            ->addTopButtons(['add' => ['size' => '60%'], 'deleteAll'])
            ->addExtraMap([['type' ,'=',$type]])
            ->addSearch([
                ['title', '文章标题搜索', 'input,Like'],
            ])->fetch();
    }

    public static function getType(): array
    {
        $type = Cache::get('news_type');
        if($type == 1){
            $list=[1 => '系统公告'];
        }else if($type == 2){
            $list=[2 => '健康管理中心'];
        }else{
            $list=[3 => '动态资讯'];
        }
        return $list;
    }
}
